'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: celery_extension.py
* @Time: 2025/9/25
* @All Rights Reserve By Brtc
'''
from celery import Task, Celery
from flask import Flask


def init_app(app:Flask):
    """Celery 配置服务器初始化"""
    class FalskTask(Task):
        """定义FlaskTask 确保Celery 在 Flask 应用上下文中运行，这样可以访问flask的配置， 数据库等内容"""
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    #1、创建Celery应用并配置
    celery_app = Celery(app.name, task_cls = FalskTask)
    celery_app.config_from_object(app.config["CELERY"])
    celery_app.set_default()
    #2、将celery挂载在app 的扩展中
    app.extensions["celery"] = celery_app

